import { request } from '../../utils/request'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    activityRecommendations: {},
    freeCourses: [],
    freeCoursesTop: [],
    hotNeighbors: [],
    hotRanking: [],
    dataList: [],
    eventRanking: [],
    showActivity: false,
    showAdd: false,
    userInfo: {},
    bannerList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (options.q && options.q.length > 0) {
      let qrCode = options.q
      const invitedUser = qrCode.substring(
        qrCode.indexOf('%3D') + 3
      )
      wx.setStorageSync(
        'invitedUser',
        invitedUser
      )
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let app = getApp()
    app.getCurrentTabbar(0, this)
    this.getActivityRecommendations()
    this.getFreeCourses()
    this.getEventRanking()
    this.getUserInfo()
    this.getHotNeighbors()
    this.getHotRanking()
    this.getActivityList()
    this.getBanners()
  },

  toUrl(item) {
    if (
      item.currentTarget.dataset.item.urlPath &&
      item.currentTarget.dataset.item.urlPath
        .length > 0
    ) {
      wx.navigateTo({
        url:
          '/' +
          item.currentTarget.dataset.item.urlPath
      })
    }
  },

  getBanners() {
    request({
      url: '/mini/api/selectTeam/banners',
      method: 'POST',
      data: {
        type: '首页'
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          bannerList: resdata.data || []
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  toSearch() {
    wx.navigateTo({
      url: '/packageSquare/search/search'
    })
  },

  addContent() {
    this.setData({
      showAdd: true
    })
  },

  onClose() {
    this.setData({
      showAdd: false
    })
  },

  getHotNeighbors() {
    request({
      url:
        '/mini/api/findNeighbor/neighborSuggestions'
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          hotNeighbors:
            resdata.data.splice(0, 2) || []
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  getHotRanking() {
    request({
      url: '/mini/api/selectTeam/hotGroup'
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          hotRanking:
            resdata.data.splice(0, 2) || []
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  getActivityList() {
    let now = new Date()
    let selectDay =
      now.getFullYear() +
      '-' +
      (now.getMonth() + 1 < 10
        ? '0' + (now.getMonth() + 1)
        : now.getMonth() + 1) +
      '-' +
      (now.getDate() < 10
        ? '0' + now.getDate()
        : now.getDate())
    request({
      url: '/mini/api/makeTime/queryActivityList',
      method: 'POST',
      data: {
        activityDate: selectDay
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          dataList:
            resdata.data.splice(0, 2) || []
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  createActivity() {
    wx.navigateTo({
      url: '/packagePlan/createEvent/createEvent'
    })
    this.setData({
      showAdd: false
    })
  },

  createCompetition() {
    wx.navigateTo({
      url:
        '/packagePlan/createCompetition/createCompetition'
    })
    this.setData({
      showAdd: false
    })
  },

  createCurriculum() {
    wx.navigateTo({
      url:
        '/packageMy/curriculumUpload/curriculumUpload'
    })
    this.setData({
      showAdd: false
    })
  },

  createWork() {
    wx.navigateTo({
      url: '/packageMy/workUpload/workUpload'
    })
    this.setData({
      showAdd: false
    })
  },

  getUserInfo() {
    request({
      url: '/mini/api/mine/getUserInfo'
    })
      .then((resdata) => {
        if (resdata.code == 200) {
          this.setData({
            userInfo: resdata.data
          })
        } else {
          this.setData({
            userInfo: {}
          })
        }
      })
      .catch((err) => {
        console.log(err)
      })
  },

  /**
   * 获取活动推荐
   */
  getActivityRecommendations() {
    request({
      url: '/mini/api/square/activitySuggestions',
      options: {
        needLogin: false
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          activityRecommendations:
            resdata.data || {}
        })
        if (
          Object.keys(
            this.data.activityRecommendations
          ).length > 0
        ) {
          this.setData({
            showActivity: true
          })
        }
      } else {
        this.setData({
          showActivity: false
        })
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  /**
   * 获取免费课程
   */
  getFreeCourses() {
    request({
      url: '/mini/api/square/freeCourses',
      method: 'POST',
      data: {
        type: ''
      },
      options: {
        needLogin: false
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          freeCourses: resdata.data || []
        })
        this.setData({
          freeCoursesTop:
            resdata.data.splice(0, 2) || []
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },
  /**
   * 获取赛事榜单
   */
  getEventRanking() {
    request({
      url: '/mini/api/square/competitionRanking',
      method: 'POST',
      data: {
        type: ''
      },
      options: {
        needLogin: false
      }
    }).then((resdata) => {
      if (resdata.code == 200) {
        this.setData({
          eventRanking: resdata.data || []
        })
      } else {
        wx.showToast({
          title: resdata.message,
          icon: 'none'
        })
      }
    })
  },

  /**
   * 查看约活动页面
   */
  toAppointmentActivity() {
    wx.navigateTo({
      url:
        '/packageSquare/appointmentActivity/index'
    })
  },
  /**
   * 查看找邻居页面
   */
  toFindNeighbors() {
    wx.navigateTo({
      url: '/packageSquare/findNeighbors/index'
    })
  },
  /**
   * 查看选队伍页面
   */
  toSelectingTeams() {
    wx.navigateTo({
      url:
        '/packageSquare/selectingTeams/selectingTeams'
    })
  },

  toRankingDetail(item) {
    wx.navigateTo({
      url:
        '/packageSquare/rankingDetail/rankingDetail?id=' +
        item.currentTarget.dataset.item.id
    })
  },

  toCoursesDetail(item) {
    wx.navigateTo({
      url:
        '/packagePlan/coursesDetail/coursesDetail?id=' +
        item.currentTarget.dataset.item.id
    })
  },

  toScheduleTime() {
    wx.navigateTo({
      url:
        '/packageSquare/scheduleTime/scheduleTime'
    })
  },

  toHomePage(item) {
    wx.navigateTo({
      url:
        '/packageMy/personalHomepage/personalHomepage?userId=' +
        item.currentTarget.dataset.userid
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
})
